package com.example.pandaso.utils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

public class ResponseUtils {
    public static ResponseEntity<?> ok(Object data) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", HttpStatus.OK.value());
        response.put("data", data);
        return ResponseEntity.ok(response);
    }

    public static ResponseEntity<?> error(HttpStatus status, String message) {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", status.value());
        response.put("message", message);
        return ResponseEntity.status(status).body(response);
    }
    
    public static void error(HttpServletResponse response, HttpStatus status, String message) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(status.value());
        Map<String, Object> error = new HashMap<>();
        error.put("code", "401");
        error.put("message", message);
        ObjectMapper mapper = new ObjectMapper();
        response.getWriter().write(mapper.writeValueAsString(error));
    }
}
